package ir;

import java.util.Stack;

public class LabelGenerator {
	private static String currentClass;
	private static String currentMethod;
	
	private static int ifCounter=0;
	private static int whileCounter=0;
	private static int stringCounter=0;
	private static int shortCircuitingCounter=0;
	
	private static Stack<String> loopStack=new Stack<String>();
	
	public static String generate(String className, String methodName){
		return String.format("%s_%s", className, methodName);
	}
	
	public static String generateMethodName(){
		return String.format("%s_%s",currentClass,currentMethod);
	}
	
	public static String generateMethodName(String className, String methodName){
		return String.format("%s_%s",className,methodName);
	}
	
	public static String generateIfLabel(){
		ifCounter++;
		return String.format("%s_%s_%s_%s","if",Integer.toString(ifCounter),currentClass,currentMethod);
	}
	
	public static String generateElseLabel(){
		return String.format("%s_%s_%s_%s","else",Integer.toString(ifCounter),currentClass,currentMethod);
	}
	
	public static String generateEndIfLabel(){
		return String.format("%s_%s_%s_%s_%s","end","if",Integer.toString(ifCounter),currentClass,currentMethod);
	}
	
	
	public static String generateWhileLabel(){
		whileCounter++;
		loopStack.push(String.format("%s_%s_%s",Integer.toString(whileCounter),currentClass,currentMethod));
		return String.format("%s_%s","while",loopStack.peek());
	}

	public static String generateEndWhileLabel(){
		return String.format("%s_%s_%s","end","while",loopStack.peek()); // without popping
	}
	
	public static String generateBreakLabel(){
		return generateEndWhileLabel();
	}
	
	public static String generateContinueLabel(){
		return String.format("%s_%s","while",loopStack.peek()); // without popping
	}
	
	public static void exitCurrentLoop(){
		loopStack.pop();
	}

	public static String getCurrentClass() {
		return currentClass;
	}

	public static void setCurrentClass(String current_class) {
		LabelGenerator.currentClass = current_class;
		ifCounter=0;
		whileCounter=0;
		stringCounter=0;
		shortCircuitingCounter=0;
		loopStack.removeAllElements();
	}

	public static String getCurrentMethod() {
		return currentMethod;
	}

	public static void setCurrentMethod(String current_method) {
		LabelGenerator.currentMethod = current_method;
		ifCounter=0;
		whileCounter=0;
		stringCounter=0;
		shortCircuitingCounter=0;
		loopStack.removeAllElements();
	}
	
	public static String generateStringLiteralLabel(String varName){
		stringCounter++;
		return String.format("stringLiteral_%s_%s_%s",Integer.toString(stringCounter),currentClass,currentMethod);
	}
	
	public static String generateRuntimeErrorLabel(RuntimeErrorType type){
		return String.format("runtimeError_%s",type.name());
	}
	
	public static String generateShortCircuitingLabel(){
		shortCircuitingCounter++;
		return String.format("andor_Label_%s_%s_%s",Integer.toString(shortCircuitingCounter),currentClass,currentMethod);
	}
	
	public static String generateEndShortCircuitingLabel_Label(){
		return String.format("end_andor_Label_%s_%s_%s",Integer.toString(shortCircuitingCounter),currentClass,currentMethod);
	}

	public static String generateClassDVLabel(String className){
		return String.format("_%s_@DV", className);
	}
}
